#$?  最后命令的执行结果




app_name='spf_amir'
[ -z "$APP_PATH" ] && APP_PATH="$HOME/spf_amix" # 这个路径不能修改
[ -z "$REPO_URI" ] && REPO_URI=""
[ -z "$REPO_URI" ] && REPO_URI="3.0"

debug_mode=1


msg() {
  printf  '%b\n' "$1" >&2
}

success() {
    if [ "$ret" -eq '0' ]; then
        msg "\33[32m[✔]\33[0m ${1}${2}"
    fi
}


error() {
    msg "\33[31m[✘]\33[0m ${1}${2}"
    exit 1
}


debug() {
    if [ "$debug_mode" -eq '1' ] &&  [ "$ret" -gt '1' ]; then
        msg "An error occurred in function \"${FUNCNAME[$i+1]}\" on line ${BASH_LINENO[$i+1]}, we're sorry for that."
    fi
}


lnif() {
    if [ -e "$1" ]; then
        ln -sf "$1" "$2"
    fi
    ret="$?"
    debug
}

create_symlinks() {
    local source_path="$1"
    local target_path="$2"
    msg "[$1] [$2]"

    lnif "$source_path/.vimrc"                  "$target_path/.vimrc"
    lnif "$source_path/.vimrc.local"            "$target_path/.vimrc.local"
    lnif "$source_path/.vimrc.bundles"          "$target_path/.vimrc.bundles"
    lnif "$source_path/.vimrc.bundles.local"    "$target_path/.vimrc.bundles.local"
    lnif "$source_path/.vim"                    "$target_path/.vim"


    ret="$?"
    success "Setting up vim symlinks."
    debug
}




do_backup() {
    if [ -e "$1" ] || [ -e "$2" ] || [ -e "$3" ]; then
        msg "Attempting to back up your original vim configuration."
        today=`date +%y%m%d_%s`
        for i in "$1" "$2" "$3"; do
            [ -e "$i" ]  &&  [ ! -L "$i"  ] &&  mv -v "$i" "$i.$today"; # -e 存在这个文件 -L 文件存在并且是个字符链接
        done
        ret="$?"
        success "Your original vim configuration has been backed up."
        debug
    fi
}


variable_set() {
    if [ -z "$1" ]; then
        error "You must have your HOME environmental variable set to continue."
    fi
}


program_must_exist() {
    program_exists $1

    # throw error on non-zero return value
    if [ "$?" -ne 0 ]; then
        error "You must have '$1' installed to continue."
    fi
}


program_exists() {
    local ret='0'
    command -v $1 >/dev/null 2>&1 || { local ret='1'; }

    # fail on non-zero return value
    if [ "$ret" -ne 0 ]; then
        return 1
    fi

    return 0

}
############################# MAIN
variable_set "$HOME"
program_must_exist  "vim"
program_must_exist "git"


do_backup   "$HOME/.vim" \
            "$HOME/.vimrc" \
            "$HOME/.gvimrc"

create_symlinks     "$APP_PATH" \
                    "$HOME"










